package com.demo.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.demo.common.vo.Result;
import com.demo.entity.Product;
import com.demo.entity.Student;
import com.demo.service.StudentService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;

/**
 * controller
 */
@RestController
@RequestMapping("/student")
public class StudentController {

    @Resource
    private StudentService studentService;

    @PostMapping("/add")
    public Result add(@RequestBody @Valid Student student){
        studentService.save(student);
        return Result.success();
    }

    @PostMapping("/delete")
    public Result delete(@RequestParam Integer id){
        studentService.removeById(id);
        return Result.success();
    }

    @PostMapping("/update")
    public Result update(@RequestBody Student student){
        if(student.getId()==null){
            return Result.error("更新记录主键不能为空");
        }
        studentService.updateById(student);
        return Result.success();
    }

    @PostMapping("/list/v2")
    public Result list1(@RequestBody Student student, @RequestBody Product product, @RequestParam(value = "name",required = false)String name){
        return Result.success(studentService.list(new QueryWrapper<>(student)));
    }

    @PostMapping("/list")
    public Result list(@RequestBody Student student){
        return Result.success(studentService.list(new QueryWrapper<>(student)));
    }

    @GetMapping("/detail")
    public Result list(@RequestParam Integer id){
        return Result.success(studentService.getById(id));
    }

}
